package com.gamota.mobi.background;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.gamota.mobi.ListingActivity.InstalledAppItem;
import com.gamota.mobi.db.DatabaseHelper;
import com.gamota.mobi.event.Event;

import de.greenrobot.event.EventBus;

public class PackageEventReceiver extends BroadcastReceiver {

	private DatabaseHelper db;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		String action = intent.getAction();
		db = new DatabaseHelper(context);
		if (action.equalsIgnoreCase(Intent.ACTION_PACKAGE_ADDED)) {
			InstalledAppItem app = new InstalledAppItem(intent.getDataString().split(":")[1], "null");
			db.addInstalledApp(app);
			EventBus.getDefault().post(new Event.AppInstalledEvent(app));
		} else if (action.equalsIgnoreCase(Intent.ACTION_PACKAGE_REMOVED)) {
			String packageName = intent.getDataString().split(":")[1];
			db.deleteInstalledApp(packageName);
			EventBus.getDefault().post(new Event.AppUninstalledEvent(new InstalledAppItem(packageName, "null")));
		}
	}

}
